package com.burgeon.backend.Rest.coupon.util;
/**
 * 通过标签查询会员
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.VipAttrib;
import com.burgeon.backend.entity.VipQuery;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindVipQueryByTags extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindVipQueryByTags(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public ArrayList<String> getData(VipAttrib vipAttrib) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(vipAttrib), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        //String count = new JSONArray(str).getJSONObject(0).getString("count");

        if (!"0".equals(code)){
            throw new Exception(str);
        }
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<String> data = new ArrayList<String>();

        for (int i = 0; i < jsonArray.length(); i++) {
            data.add(jsonArray.getJSONArray(i).getString(0));
        }
        return data;
    }

    private MultiValueMap Data(VipAttrib vipAttrib) {

        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段
        columns.add("'C_VIP_ID;CARDNO'");

        params.put("columns", columns);
        params.put("table", "C_VIPATTRIB");//表名

        HashMap paramss = new HashMap();

        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();

        HashMap expr1v1 = new HashMap();
        HashMap expr1v2 = new HashMap();
        HashMap expr2v1 = new HashMap();
        HashMap expr2v2 = new HashMap();

        HashMap expr1v1v1 = new HashMap();
        HashMap expr1v1v2 = new HashMap();
        HashMap expr1v2v1 = new HashMap();
        HashMap expr1v2v2 = new HashMap();
        HashMap expr2v1v1 = new HashMap();
        HashMap expr2v1v2 = new HashMap();
        HashMap expr2v2v1 = new HashMap();
        HashMap expr2v2v2 = new HashMap();

        paramss.put("combine", "and");
        paramss.put("expr1",expr1);
        paramss.put("expr2",expr2);

        expr1.put("combine","and");
        expr1.put("expr1",expr1v1);
        expr1.put("expr2",expr1v2);
        expr2.put("combine","and");
        expr2.put("expr1",expr2v1);
        expr2.put("expr2",expr2v2);

        expr1v1.put("combine","and");
        expr1v1.put("expr1",expr1v1v1);
        expr1v1.put("expr2",expr1v1v2);
        expr1v2.put("combine","and");
        expr1v2.put("expr1",expr1v2v1);
        expr1v2.put("expr2",expr1v2v2);
        expr2v1.put("combine","and");
        expr2v1.put("expr1",expr2v1v1);
        expr2v1.put("expr2",expr2v1v2);
        expr2v2.put("combine","and");
        expr2v2.put("expr1",expr2v2v1);
        expr2v2.put("expr2",expr2v2v2);

        expr1v1v1.put("column","'C_VIPATTRIB1_ID;NAME'");//1
        expr1v1v1.put("condition", "'"+vipAttrib.getVipAttrib1()+"'");

        HashMap expr1v1v2v1 = new HashMap();
        HashMap expr1v1v2v2 = new HashMap();
        expr1v1v2v1.put("column","'C_VIPATTRIB2_ID;NAME'");//2
        expr1v1v2v1.put("condition","'"+vipAttrib.getVipAttrib2()+"'");
        expr1v1v2v2.put("column","'C_VIPATTRIB3_ID;NAME'");//3
        expr1v1v2v2.put("condition","'"+vipAttrib.getVipAttrib3()+"'");

        expr1v1v2.put("combine","and");
        expr1v1v2.put("expr1",expr1v1v2v1);
        expr1v1v2.put("expr2",expr1v1v2v2);

        HashMap expr1v2v2v1 = new HashMap();
        HashMap expr1v2v2v2 = new HashMap();
        HashMap expr1v2v1v1 = new HashMap();
        HashMap expr1v2v1v2 = new HashMap();
        expr1v2v2v1.put("column","'C_VIPATTRIB4_ID;NAME'");//4
        expr1v2v2v1.put("condition","'"+vipAttrib.getVipAttrib4()+"'");
        expr1v2v2v2.put("column","'C_VIPATTRIB5_ID;NAME'");//5
        expr1v2v2v2.put("condition","'"+vipAttrib.getVipAttrib5()+"'");
        expr1v2v1v1.put("column","'C_VIPATTRIB6_ID;NAME'");//6
        expr1v2v1v1.put("condition","'"+vipAttrib.getVipAttrib6()+"'");
        expr1v2v1v2.put("column","'C_VIPATTRIB7_ID;NAME'");//7
        expr1v2v1v2.put("condition", "'"+vipAttrib.getVipAttrib7()+"'");

        expr1v2v1.put("combine","and");
        expr1v2v1.put("expr1",expr1v2v1v1);
        expr1v2v1.put("expr2",expr1v2v1v2);
        expr1v2v2.put("combine","and");
        expr1v2v2.put("expr1",expr1v2v2v1);
        expr1v2v2.put("expr2",expr1v2v2v2);

        HashMap expr2v1v1v1 = new HashMap();
        HashMap expr2v1v1v2 = new HashMap();
        expr2v1v1v1.put("column","'C_VIPATTRIB8_ID;NAME'");//8
        expr2v1v1v1.put("condition","'"+vipAttrib.getVipAttrib8()+"'");
        expr2v1v1v2.put("column","'C_VIPATTRIB9_ID;NAME'");//9
        expr2v1v1v2.put("condition","'"+vipAttrib.getVipAttrib9()+"'");

        HashMap expr2v1v2v1 = new HashMap();
        HashMap expr2v1v2v2 = new HashMap();
        expr2v1v2v1.put("column","'C_VIPATTRIB10_ID;NAME'");//10
        expr2v1v2v1.put("condition","'"+vipAttrib.getVipAttrib10()+"'");
        expr2v1v2v2.put("column","'C_VIPATTRIB11_ID;NAME'");//11
        expr2v1v2v2.put("condition","'"+ vipAttrib.getVipAttrib11()+"'");

        expr2v1v1.put("combine","and");
        expr2v1v1.put("expr1",expr2v1v1v1);
        expr2v1v1.put("expr2",expr2v1v1v2);
        expr2v1v2.put("combine","and");
        expr2v1v2.put("expr1",expr2v1v2v1);
        expr2v1v2.put("expr2",expr2v1v2v2);

        HashMap expr2v2v1v1 = new HashMap();
        HashMap expr2v2v1v2 = new HashMap();
        expr2v2v1v1.put("column","'C_VIPATTRIB12_ID;NAME'");//12
        expr2v2v1v1.put("condition","'"+ vipAttrib.getVipAttrib12()+"'");
        expr2v2v1v2.put("column","'C_VIPATTRIB13_ID;NAME'");//13
        expr2v2v1v2.put("condition","'"+ vipAttrib.getVipAttrib13()+"'");

        HashMap expr2v2v2v1 = new HashMap();
        HashMap expr2v2v2v2 = new HashMap();
        expr2v2v2v1.put("column","'C_VIPATTRIB14_ID;NAME'");//14
        expr2v2v2v1.put("condition","'"+vipAttrib.getVipAttrib14()+"'");
        expr2v2v2v2.put("column","'C_VIPATTRIB15_ID;NAME'");//15
        expr2v2v2v2.put("condition","'"+ vipAttrib.getVipAttrib15()+"'");

        expr2v2v1.put("combine","and");
        expr2v2v1.put("expr1",expr2v2v1v1);
        expr2v2v1.put("expr2",expr2v2v1v2);
        expr2v2v2.put("combine","and");
        expr2v2v2.put("expr1",expr2v2v2v1);
        expr2v2v2.put("expr2",expr2v2v2v2);

        params.put("params",paramss);
        params.put("count","true");
        params.put("range","50000");

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindVipQueryByTags g = new FindVipQueryByTags("39.100.45.8:55322", "nea@burgeon.com.cn", "123");
        try {

            VipAttrib data = new VipAttrib();
            data.setVipAttrib1("160-165");

            g.getData(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
